package com.see51.sdk.java;

import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created by saint on 2019/2/25.
 */

//android 麦克风采集 pcm 16k 16bit
public class See51PcmRecord implements Runnable
{
    private AudioRecord audioRecord;
    private int bufferSize;
    private Context context;
    private ArrayBlockingQueue<byte[]> audioFrames;
    private boolean bStop = false;
    private int nVolume = 0;
    //采样率
    public static  int         RECORDER_SAMPLERATE =16000;
    private static final int  RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int  RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    public See51PcmRecord(Context context)
    {

        this.context =context;
        bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);
        if(bufferSize < 2048)
            bufferSize = 2048;


        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);
        audioFrames = new ArrayBlockingQueue<byte[]>(1024);
    }

    //建议不要超过60s
    public void start()
    {
        audioFrames.clear();
        bStop = false;
    }

    private int computeVolume(byte[] audiodata, int nLength)
    {
        int max = 0;
        int temp = 0;
        for (int i = 0; i < nLength/2; i+=2)
        {
            temp = (audiodata[i] + 256 * audiodata[i+1]);
            if(Math.abs(temp) > max)
                max = Math.abs(temp);
        }
        return max;
    }
    //public  objre
    @Override
    public void run()
    {






        // TODO Auto-generated method stub
        try {
            audioRecord.startRecording();
            Log.d("Pcm", "AACRecord：Ok");
        } catch (IllegalStateException e)
        {
            Log.d("Pcm", "AACRecord：err");
            e.printStackTrace();
            audioRecord.release();
            audioRecord = null;
            return;
        }
        int read = 0;

        while (!bStop) {
            byte[] data= new byte[bufferSize];
            try {
                read = audioRecord.read(data, 0, bufferSize);//采集音频
            }catch (RuntimeException e) {
                e.printStackTrace();
            }
            //Log.i("TestAAC", "AACRecord：采集音频");
            nVolume = computeVolume(data, read);
            if (AudioRecord.ERROR_INVALID_OPERATION != read) {
                try {
                    //Log.i("TestAAC", "AACRecord：添加到队列");
                    audioFrames.add(data);
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        }

        if(audioRecord != null) {
            audioRecord.stop();
            audioRecord.release();
            audioRecord = null;
        }
    }

    //返回pcm 数据 16k ，16bit
    public ArrayBlockingQueue<byte[]> stop(){
        bStop = true;
        return audioFrames;
    }
}
